﻿using NLog;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using WindowsFormsApp.utils;

namespace WindowsFormsApp
{
    public partial class ConvertToolsForm : Form
    {

        Logger logger = LogManager.GetCurrentClassLogger();
        
        public ConvertToolsForm()
        {
            InitializeComponent();
            InitData();
            logger.Info("启动成功");
        }

        private void InitData()
        {

            List<string> items = new List<string> { "dev", "fat", "pre", "pre_temp", "uat", "prod" };
            comboBox1.DataSource = items;
            textBox3.Text = System.DateTime.Now.ToString("yyyyMMdd");

        }

        /*
         * 转换JSON
         */
        private async void button1_Click(object sender, EventArgs e)
        {

            string url = textBox1.Text;
            if (string.IsNullOrEmpty(url)) {

                textBox2.Text = "请输入需要处理的链接";
                return;
            }
            string version = textBox3.Text;
            if (string.IsNullOrEmpty(version))
            {

                textBox2.Text = "请输入版本号";
                return;
            }
            string inputBranch = comboBox1.SelectedItem?.ToString(); 
            if (string.IsNullOrEmpty(inputBranch))
            {
                textBox2.Text = "请输入执行环境";
                return;
            }
            textBox2.Text = await UrlConvertUtil.convertUrl(url, inputBranch, version);
            textBox4.Text = "";
        }

        /*
         * 执行接口
         */
        private async void button3_Click(object sender, EventArgs e)
        {
            string version = textBox3.Text;
            if (string.IsNullOrEmpty(version))
            {

                textBox4.Text = "请输入版本号";
                return;
            }
            string inputBranch = comboBox1.SelectedItem?.ToString();
            if (string.IsNullOrEmpty(inputBranch))
            {
                textBox4.Text = "请输入执行环境";
                return;
            }

            string content = textBox2.Text;
            if (string.IsNullOrEmpty(content))
            {
                textBox4.Text = "请输入需要转换的结果";
                return;
            }
            string token = textBox5.Text;
            if (string.IsNullOrEmpty(token))
            {
                token = await UrlConvertUtil.getToken(inputBranch);
                textBox5.Text = token;
            }
            if ("uat,prod".Contains(inputBranch))
            {
                DialogResult rs = MessageBox.Show(
                    "确认执行" + inputBranch + "环境吗？请检查版本号和执行的内容",
                    "执行确认",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2
                );

                if (rs == DialogResult.Yes)
                {
                    textBox4.Text = await UrlConvertUtil.submit(textBox1.Text, inputBranch, token, content, checkBox1.Checked);
                    MessageBox.Show("操作已执行！", "完成");
                }
                else
                {
                    MessageBox.Show("操作已取消", "提示");
                }
            }
            else {
                textBox4.Text = await UrlConvertUtil.submit(textBox1.Text, inputBranch, token, content, checkBox1.Checked);
            }
            logger.Info("请求执行接口成功");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            string version = textBox3.Text;
            string inputBranch = comboBox1.SelectedItem?.ToString();
            if (string.IsNullOrEmpty(inputBranch))
            {
                textBox4.Text = "请输入执行环境";
                return;
            }

            string content = textBox2.Text;
            if (string.IsNullOrEmpty(content))
            {
                textBox4.Text = "请输入需要转换的结果";
                return;
            }
            string token = textBox5.Text;
            if (string.IsNullOrEmpty(token))
            {
                token = await UrlConvertUtil.getToken(inputBranch);
                textBox5.Text = token;
            }
            textBox4.Text = await UrlConvertUtil.query(textBox1.Text, inputBranch, token, content, checkBox1.Checked);
            logger.Info("请求查询接口成功");
        }
    }
}
